﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;

namespace RAgency.Manager
{
    public partial class MSelection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetUsers();
            }
        }

        private void GetUsers()
        {
            UsersDDL.DataSource = Roles.GetUsersInRole("user");
            UsersDDL.DataBind();
        }

        private void SaveResults(string username)
        {
            if (username != "")
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand("insert into results (id, result) values ((select id from users where (login='" + username + "')), @x1)", conn);
                comm.Parameters.Add("@x1", System.Data.SqlDbType.NVarChar).Value = this.Session["Text"];
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }

        protected void SelectClick(object sender, EventArgs e)
        {
            bool flag = false;
            string query = "select * from profile";
            if (EducationChk.Checked | DutyChk.Checked | ExpChk.Checked | AgeChk.Checked)
            {
                query += " where";
            }
            if (EducationChk.Checked)
            {
                query += " (education='" + Education_d.Text + "')";
                flag = true;
            }
            if (DutyChk.Checked)
            {
                if (flag) query += " and";
                query += " (duty='" + Duty_d.Text + "')";
                flag = true;
            }
            if (ExpChk.Checked && ExpTxt.Text != "")
            {
                if (flag) query += " and";
                query += " (exp>=" + ExpTxt.Text + ")";
                flag = true;
            }
            if (AgeChk.Checked && MinAge.Text != "" && MaxAge.Text != "")
            {
                if (flag) query += " and";
                query += " (age>" + MinAge.Text + ") and (age<" + MaxAge.Text + ")";
            }
            if (!flag) { query += " where (createdate<'" + DateTime.Now + "')"; }
            else { query += " and (createdate<'" + DateTime.Now + "')"; }
            this.Session["Text"] = query;
            if (UserChk.Checked) SaveResults(UsersDDL.Text);
            Response.Redirect("~/Manager/MSelectView.aspx");
        }
    }
}